// @ts-nocheck
import {createRouter, createWebHashHistory, createWebHistory} from "vue-router";
import config from '@/config.ts'
import store from 'store'
import {routerConfig} from './config.ts'
import {defineStore} from 'pinia'
import {useSystem} from "@/stores/system/index"
import {getRouters} from "@/api/user.ts";

// import {useRouter} from 'vue-router'
// const router = useRouter

import {createRouter, createWebHashHistory, createWebHistory} from 'vue-router'
import layout from "@/pages/home/layout.vue";
const modules = import.meta.glob("../pages/home/**/**/index.vue")

const router = createRouter({
    history:createWebHistory(), //createWebHashHistory(),
    routes:routerConfig()
})
//配合若依系统，动态添加路由
export const ry_addRouter = async () => {
    //路由数据已经存在
    let routerInfo = await getRouters()
    store.set('routerInfo', routerInfo.data)
    console.log(routerInfo,'routerInfo')
    const ry_routers = routerInfo.data
    for (let i in ry_routers) {
        //没有删除原先路由前，先做的配置
        // console.log(ry_routers[i])
        if (ry_routers[i].meta.title !== '系统管理' || ry_routers[i].meta.title !== '系统管理' || ry_routers[i].meta.title !== '系统工具'){
            let newRouter = formatRouterData(ry_routers[i], 'home')
        }
    }

    console.log(router.getRoutes(),'动态路由')
    //格式化路由格式
    function formatRouterData(r, parentName) {
        

        let name = r.path.replace(/\//g, "_")
        let route = {
            name: name,
            path: r.path,
            meta: r.meta
        }
        if (r.component !== 'Layout'){
            let components = Object.keys(modules).filter(item=>{
                return item.match(r.component.replace('/src',''))
            })
            if(components.length == 0){
                return
            }
            route.component = () => import(/* @vite-ignore */components[0])
        }else{
            route.component = layout
        }
        router.addRoute(parentName, route)
        // tell.log(parentName, name, r.path, r.component)
        if (r.children) {//存在子路由
            for (let i in r.children) {
                formatRouterData(r.children[i], name)
            }
        }
    }
}

router.beforeEach((to, from, next) => {
    let title = to.meta.title
    // if (title && to.meta.commons){ //dev test
    if (title) {
        let webtitle = document.querySelector('title')
        webtitle.textContent = title + '-' + config.webTitle
        store.set('lastRoute', to)

        //统计路由地址
        // let commons = store.get('commons') || []
        // commons = Array.from(commons)
        // //去重
        // let ifReplay = true
        // for (let i in commons){
        //     if (commons[i].title === title){
        //         ifReplay = false
        //         break;
        //     }
        // }
        // if (ifReplay){
        //     if (commons.length === 12) commons.shift()
        //     // console.log('to', to)
        //     let item  = {
        //         href:to.href,
        //         title:to.meta.title
        //     }
        //     commons.push(item)
        //     store.set('commons', commons)
        // }
    }
    // console.log(to)
    next()
})
router.afterEach((to, from, next) => {
    //TABS
    useSystem().saveTabs(to)
    // useSystem.saveTabs()
    const storeSystem = useSystem()
    storeSystem.saveIndexWatch(Date.now())
})
router.onError((err)=>{
    tell.log('路由错误', err)
})
export default router